//
//  OrderFillViewController.m
//  SXH
//
//  Created by Ryan on 16/3/17.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "OrderFillViewController.h"
#import "HeadTableViewCell1.h"
#import "TicketholderCell.h"
#import "TouristinfoSection.h"
#import "TouristinfoCell.h"
#import "SpecialTouristSection.h"
#import "OrderHeaderView.h"
#import "SafePayViewController.h"
#import "SpecialTouristCell.h"
#import "EditTouristsListViewController.h"
#import "CommonService.h"
#import "LibController.h"
#import "OrderProductEntity.h"
#import "OrderEntity.h"
#import "RouteDetailModel.h"
typedef enum : NSUInteger {
    OrderFill_Head = 0,
    OrderFill_TicketholderCell,
    OrderFill_gary1,
    OrderFill_touristinfo,
    OrderFill_gary2,
    OrderFill_SpecialTourist
} OrderFillVCTag;
//@interface TicketholderModel()


@interface OrderFillViewController ()<TouristinfoCellDelegate,SpecialTouristSectionDelegate,TouristinfoSectionDelegate>

@property(nonatomic,strong) TouristinfoSection *touristinfoSection;
@property(nonatomic,strong) SpecialTouristSection *specialTouristSection;
@property(nonatomic,strong) OrderHeaderView *headerView;
@property(nonatomic,strong) NSMutableArray *touristinfoArray;
@property(nonatomic,strong) NSMutableArray *specialTouristArray;
@property(nonatomic,strong) NSMutableArray *touristTextFields;

@end

#define K_touristInfoWrong  @"游客信息不正确"
#define K_allTouristWrong   @"请填写所有游客信息"
#define K_nameEnterTip      @"请输入姓名"

#define K_headCellIdentifier  @"HeadTableViewCell1"
#define K_specialTouristCellIdentifier  @"SpecialTouristCell"
#define K_ticketholderCellIdentifier    @"TicketholderCell"
#define K_garyCellIdentifier         @"garycell"
#define K_touristinfoCellIdentifier    @"TouristinfoCell"

@implementation OrderFillViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.ticketHolderArray = [[NSMutableArray alloc]initWithObjects:
                                  @{@"预定姓名":@"联系人姓名"},
                                  @{@"联系号码":@"联系人手机"},
                                  @{@"推荐人":@"推荐人ID或者手机号"},
                                  nil];
        self.touristinfoArray = [[NSMutableArray alloc]init];
        self.specialTouristArray = [[NSMutableArray alloc]initWithObjects:@"", nil];
        self.touristTextFields = [[NSMutableArray alloc]init];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Observe keyboard hide and show notifications to resize the text view appropriately.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    self.view.backgroundColor = [UIColor colorWithRed:0.941 green:0.941 blue:0.941 alpha:1.00];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    
    self.headerView = [[[NSBundle mainBundle]loadNibNamed:@"OrderHeaderView" owner:self options:nil]lastObject];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@62);
        make.height.equalTo(@29);
    }];
    [self.headerView.closeBtn addTarget:self action:@selector(closeHeaderView) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(self.headerView.mas_bottom);
        make.bottom.equalTo(@(-55));
    }];

    self.totalPrice.text = self.price;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case OrderFill_Head:
            return 110;
            break;
        case OrderFill_TicketholderCell:
            return 50;
            break;
        case OrderFill_gary1:
            return 10;
            break;
        case OrderFill_touristinfo:
            return 66;
            break;
        case OrderFill_gary2:
            return 10;
            break;
        case OrderFill_SpecialTourist:
            return 43;
            break;
        case 6:
            return 1;
            break;
        default:
            break;
    }
    return 0;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//    if (section == 3 ||section == 5) {
//        return 44;
//    }
    if (section == 3) {
        return 87;
    }
    else
        return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case OrderFill_Head:
            return 1;
            break;
        case OrderFill_TicketholderCell:
            return 3;
            break;
        case OrderFill_gary1:
        case OrderFill_gary2:
            return 1;
            break;
        case OrderFill_touristinfo:
            return [self.touristinfoArray count];
            break;
        case OrderFill_SpecialTourist:
//            if (self.specialTouristSection.isExpand)
//            {
//                return [self.specialTouristArray count];
//            }
//            else
//            {
//                return 0;  
//            }
            return 0;
            break;
        case 6:
            return 0;
            break;
        default:
            break;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case OrderFill_Head:
        {
            NSString *cellId = K_headCellIdentifier;
            HeadTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
            }
            cell.headTitle1.text = self.tourismDetailsEntity.t_name;
            cell.headTitle2.text = [NSString stringWithFormat:@"<%@>",self.selectTime];
            cell.travelAgencyName.text = self.tourismDetailsEntity.a_name;
            return cell;
        }
            break;
        case OrderFill_TicketholderCell:
        {
            NSString *cellId = K_ticketholderCellIdentifier;
            TicketholderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
                if (indexPath.row == 2 && self.saler_phone.length > 0)
                {
                    cell.contentLabel.text = self.saler_phone;
                    cell.contentLabel.userInteractionEnabled = NO;
                }
                [self.touristTextFields addObject:cell.contentLabel];
            }
            NSDictionary *dictionary = [self.ticketHolderArray objectAtIndex:indexPath.row];
            cell.titleLabel.text = [[dictionary allKeys]firstObject];
            cell.contentLabel.placeholder = [dictionary objectForKey:[[dictionary allKeys]firstObject]];
            return cell;
        }
            break;
        case OrderFill_gary1:
        case OrderFill_gary2:
        {
            NSString *cellId = K_garyCellIdentifier;
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            cell.frame = CGRectMake(0, 0, ScreenWidth, 10);
            cell.backgroundColor = [UIColor colorWithRed:0.941 green:0.941 blue:0.941 alpha:1.00];
            return cell;
        }
            break;
        case OrderFill_touristinfo:
        {
            NSString *cellId = K_touristinfoCellIdentifier;
            TouristinfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.delegate = self;
            }
            cell.model = [self.touristinfoArray objectAtIndex:indexPath.row];
            return cell;
        }
            break;
        case OrderFill_SpecialTourist:
        {
            NSString *cellId = K_specialTouristCellIdentifier;
            SpecialTouristCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
                cell.is_Select = NO;
            }
            return cell;
        }
            break;
        case 6:
        {
            NSString *cellId = K_headCellIdentifier;
            HeadTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
            }
            return cell;
        }
            break;
        default:
            break;
    }

    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 3)
    {
        self.touristinfoSection = [[[NSBundle mainBundle]loadNibNamed:@"TouristinfoSection" owner:self options:nil]lastObject];
        self.touristinfoSection.delegate = self;
        self.touristinfoSection.totalAdultNumLabel.text = [NSString stringWithFormat:@"%zi",_adultTotalNum];
        self.touristinfoSection.totalChildNumLabel.text = [NSString stringWithFormat:@"%zi",_childTotalNum];
        self.touristinfoSection.totalOlderNumLabel.text = [NSString stringWithFormat:@"%zi",_olderTotalNum];
        self.touristinfoSection.totalChild_NoBedNumLabel.text = [NSString stringWithFormat:@"%zi",_child_NoBedTotalNum];
        self.touristinfoSection.totalBabyNumLabel.text = [NSString stringWithFormat:@"%zi",_babyTotalNum];

        NSInteger  selectAdultNum = 0;
        NSInteger  selectChildNum = 0;
        NSInteger  selectOlderNum = 0;
        NSInteger  selectChild_NoBed_Num = 0;
        NSInteger  selectBabyNum = 0;

        for (EditTouristModel * model  in self.touristinfoArray) {
            switch ([model.identity intValue]) {
                case 1:
                    selectAdultNum ++;
                    break;
                case 2:
                    selectOlderNum ++;
                    break;
                case 3:
                    selectChild_NoBed_Num ++;
                    break;
                case 4:
                    selectChildNum ++;
                    break;
                case 5:
                    selectBabyNum ++;
                    break;
                    
                default:
                    break;
            }
        }
        self.touristinfoSection.selectAdultNumLabel.text = [NSString stringWithFormat:@"%zi",selectAdultNum];
        self.touristinfoSection.selectChildNumLabel.text = [NSString stringWithFormat:@"%zi",selectChildNum];
        self.touristinfoSection.selectOlderNumLabel.text = [NSString stringWithFormat:@"%zi",selectOlderNum];
        self.touristinfoSection.selectChild_NoBedNumLabel.text = [NSString stringWithFormat:@"%zi",selectChild_NoBed_Num];
        self.touristinfoSection.selectBabyNumLabel.text = [NSString stringWithFormat:@"%zi",selectBabyNum];


        return self.touristinfoSection;
    }
//    else if(section == 5)
//    {
//        if (!self.specialTouristSection)
//        {
//            self.specialTouristSection = [[[NSBundle mainBundle]loadNibNamed:@"SpecialTouristSection" owner:self options:nil]lastObject];
//            self.specialTouristSection.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//            self.specialTouristSection.delegate = self;
//        }
// 
//        UIView *view = [[UIView alloc] initWithFrame:[self.specialTouristSection frame]];
//        [view addSubview:self.specialTouristSection];
//        return view;
//    }
    else
        return nil;
}


- (UITableView*)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource =self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)closeHeaderView
{
    [UIView animateWithDuration:0.25 animations:^{
        self.headerView.frame = CGRectMake(0, 62, SCREEN_WIDTH, 0);
        self.tableView.frame = CGRectMake(0, 62, SCREEN_WIDTH, self.tableView.frame.size.height + 29);
    } completion:^(BOOL finished) {
        self.headerView.hidden = YES;
        [self.headerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(@62);
            make.height.equalTo(@0);
        }];
    }];
}

- (IBAction)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)deleteCell:(TouristinfoCell *)cell
{
    NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];
    [self.touristinfoArray removeObjectAtIndex:indexpath.row];
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView reloadData];
    [self.tableView endUpdates];
}

- (IBAction)gotoPay:(id)sender
{
    UITextField *ticketHolderNameField = [self.touristTextFields objectAtIndex:0];
    NSString *ticketHolderName = ticketHolderNameField.text;

    UITextField *ticketHolderPhoneField = [self.touristTextFields objectAtIndex:1];
    NSString *ticketHolderPhone = ticketHolderPhoneField.text;
    
    UITextField *ticketHolderRecommderField = [self.touristTextFields objectAtIndex:2];
    NSString *ticketHolderRecommder = ticketHolderRecommderField.text;
    
    if (ticketHolderName.length <= 0) {
        ALERT_ERR_MSG(K_nameEnterTip);
        return;
    }
    
    if (![LibController validateMobile:ticketHolderPhone]) {
        ALERT_ERR_MSG(TelEnterWrong);
        return;
    }
    
    if (!ticketHolderRecommder) {
        ticketHolderRecommder = @"";
    }
    if (self.touristinfoSection.selectAdultNumLabel.text.intValue != self.touristinfoSection.totalAdultNumLabel.text.intValue
        ||self.touristinfoSection.selectChildNumLabel.text.intValue != self.touristinfoSection.selectChildNumLabel.text.intValue)
    {
        ALERT_ERR_MSG(K_touristInfoWrong);
        return;
    }
    
    NSMutableString * tour_user_ids = [NSMutableString stringWithString:@""];
    if ([self.touristinfoArray count] != (_adultTotalNum + _childTotalNum + _babyTotalNum + _olderTotalNum + _child_NoBedTotalNum)) {
        ALERT_ERR_MSG(K_allTouristWrong);
        return;
    }
    else
    {
        for (int i = 0 ; i < [self.touristinfoArray count]; i++)
        {
            EditTouristModel *model = [self.touristinfoArray objectAtIndex:i];
            [tour_user_ids appendString:model.id];
            if (i < ([self.touristinfoArray count] - 1))
            {
                [tour_user_ids appendString:@","];
            }
        }
    }
    NSString *departure_id = @"";
    for (RouteDetailModel *detailModel in self.tourismDetailsEntity.t_departure_date_list) {
        if ([detailModel.dep_time isEqualToString:self.selectTime]) {
            departure_id = detailModel.id;
        }
    }
    
    CommonService *service = [[CommonService alloc]init];
    NSDictionary * parm = @{@"phone_type":@"1",
                            @"park_id":@"0",
                            @"user_id":USERID,
                            @"product_class_id":@"3",
                            @"product_id":self.tourismDetailsEntity.t_id,
                            @"qty":@"1",
                            @"departure_date":self.selectTime,
                            @"nickname":ticketHolderName,
                            @"phone":ticketHolderPhone,
                            @"adult_num":[NSString stringWithFormat:@"%zi",_adultTotalNum],
                            @"aged_num":[NSString stringWithFormat:@"%zi",_olderTotalNum],
                            @"child_a_num":[NSString stringWithFormat:@"%zi",_child_NoBedTotalNum],
                            @"child_b_num":[NSString stringWithFormat:@"%zi",_childTotalNum],
                            @"baby_num":[NSString stringWithFormat:@"%zi",_babyTotalNum],
                            @"recommender_phone":ticketHolderRecommder,
                            @"tour_user_ids":tour_user_ids,
                            @"departure_id":departure_id
                            };
    [service getNetWorkData:parm Path:USER_ORDER_NEW_CREATE Successed:^(NSDictionary * entity) {
        if ( [[entity objectForKey:@"code"]integerValue] == 0)
        {
            SafePayViewController *mvc = [[SafePayViewController alloc]init];
            mvc.orderIdString = [entity objectForKey:@"order_num"];
            mvc.personString = [NSString stringWithFormat:@"%zi 成人 %zi 儿童",self.adultTotalNum,self.childTotalNum];
            mvc.ticketHolderNameString = ticketHolderName;
            mvc.ticketHolderPhoneString = ticketHolderPhone;
            mvc.selectTime = self.selectTime;
            mvc.tourismDetailsEntity = self.tourismDetailsEntity;
            mvc.order_dictionary = [[NSMutableDictionary alloc]initWithDictionary:entity];
            [self.navigationController pushViewController:mvc animated:YES];
            [self orderInfo:entity total_amount:[entity objectForKey:@"real_total_amount"]];

        }
        else
        {
            ALERT_ERR_MSG([entity objectForKey:@"msg"]);
        }
    } Failed:^(NSInteger errorCode, NSString *message) {
        ALERT_ERR_MSG(message);
    }];
    
    

}

-(void)orderInfo:(NSDictionary*)dic total_amount:(NSString*)real_total_amount{
    OrderProductEntity *orderProductEntity = [[OrderProductEntity alloc] init];
    OrderEntity *orderEntity = [[OrderEntity alloc]init];
    if(self.tourismDetailsEntity){
        orderProductEntity.product_class = @"3";
//        orderProductEntity.product_id = productEntity.product_id;
        orderProductEntity.product_name = self.tourismDetailsEntity.t_name;
//        orderProductEntity.price = productEntity.sale_price;
//        orderProductEntity.introduce = productEntity.descriptionInfo;
        orderProductEntity.count = @"1";
        orderProductEntity.album_thumb = self.tourismDetailsEntity.t_image_url;
        orderProductEntity.tourism_data = @{@"departure_date":self.selectTime,
                                            @"total_people":[NSString stringWithFormat:@"%zi",[self.touristinfoArray count]]};
        orderEntity.ID = [dic objectForKey:@"order_id"];
        orderEntity.total_amount = real_total_amount;
        orderEntity.status = @"1";
        orderEntity.order_num =
//        orderEntity.payWay = _payWay;
//        orderEntity.ship_cost = productEntity.ship_cost;
        orderEntity.order_class = @"3";
        orderEntity.store_id = @"LY_ID";
        orderEntity.store_name = self.tourismDetailsEntity.a_name;
    }
    
    NSDate * nowDate = [NSDate date];
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init];
    //    dateFormat = @"yyyy年MM月dd日 HH:mm:SS";
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    orderEntity.insert_time = [dateFormat stringFromDate:nowDate];
    orderEntity.order_num = [dic objectForKey:@"order_num"];;
    
    orderEntity.product_list = [[NSMutableArray alloc] init];
    [orderEntity.product_list addObject:orderProductEntity];
    
    //TODO: 保存订单号到本地
    NSUserDefaults *orderDefault = [NSUserDefaults standardUserDefaults];
    NSArray *arrTemp=[orderDefault objectForKey:@"OrderEntityList"];
    //可以用alloc 方法代替
    NSMutableArray *mutableArray=[NSMutableArray arrayWithArray:arrTemp];
    if([mutableArray count]>0){
        for(int i=0;i<[mutableArray count];i++){
            OrderEntity *orderEntityTemp = [NSKeyedUnarchiver unarchiveObjectWithData:[mutableArray objectAtIndex:i]];
            if([orderEntityTemp.order_class isEqualToString:orderEntity.order_class]){
                [mutableArray removeObjectAtIndex:i];
            }
        }
    }
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:orderEntity];
    [mutableArray addObject:data];
    //记住要转换成不可变数组类型
    NSArray * array = [NSArray arrayWithArray:mutableArray];
    [orderDefault setObject:array forKey:@"OrderEntityList"];
}

//特殊出游人收放
- (void)sectiondidSelect:(BOOL)isExpand
{
    [self.tableView beginUpdates];
    NSMutableArray *indexPathArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self.specialTouristArray count] ; i++)
    {
        [indexPathArray addObject:[NSIndexPath indexPathForRow:i inSection:5]];
    }
    if (!isExpand)
    {
        [self.tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else
    {
        [self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
    }

    [self.tableView endUpdates];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
}

- (void)editTouristinfo
{
    EditTouristsListViewController *mvc = [[EditTouristsListViewController alloc]init];
//    mvc.totalNum = self.adultTotalNum + self.childTotalNum;
    mvc.adultNum = self.adultTotalNum;
    mvc.childNum = self.childTotalNum;
    mvc.olderNum = self.olderTotalNum;
    mvc.child_No_Bed_Num = self.child_NoBedTotalNum;
    mvc.babyNum = self.babyTotalNum;
    mvc.selectTouristArray = self.touristinfoArray;
    [mvc returnListModel:^(NSMutableArray *modelArray) {
        [self.tableView reloadData];
    }];
    [self.navigationController pushViewController:mvc animated:YES];
}


- (void)keyboardWillShow:(NSNotification *)notification {
    
    
    
    NSDictionary *userInfo = [notification userInfo];
    
    // Get the origin of the keyboard when it's displayed.
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
    CGFloat keyboardTop = keyboardRect.origin.y;
    
    for (int i = 0; i < [self.touristTextFields count]; i ++)
    {
        UITextField *textfield = [self.touristTextFields objectAtIndex:i];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:OrderFill_TicketholderCell];
        if ([textfield isFirstResponder])
        {
            CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath];
            CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
            CGFloat bottom_height  = SCREEN_HEIGHT - rectInSuperview.origin.y;
            if (keyboardTop > bottom_height) {
                self.tableView.layer.transform = CATransform3DMakeTranslation(0, bottom_height - keyboardTop , 0);

            }
        }
    }

    // Get the duration of the animation.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    
    [UIView commitAnimations];
}


- (void)keyboardWillHide:(NSNotification *)notification {
    
    NSDictionary* userInfo = [notification userInfo];
    
    
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    
        self.tableView.layer.transform = CATransform3DIdentity;
    
    [UIView commitAnimations];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
}


@end
